#coding=utf-8

import requests
import json

requests.adapters.DEFAULT_RETRIES = 5


s = requests.Session()
cookie = s.cookies.get_dict()


#1、get请求
# r = requests.get("https://httpbin.org/ip")
# print(r.text)
# print(r.content)
# print(r.json())

#2、get请求，带参数
# res = requests.get("https://httpbin.org/get",params={"user":"Hunter","passwd":"test123"})
# print(res.status_code)
# print(res.text)
# print(res.url)

#3、自定义请求头
# header = {
#     "User-Agent": "python-requests/2.26.0",
#     "token":"test123"
#   }
# re = requests.get("https://httpbin.org/get",headers=header)
# print(re.text)

#4、发送请求时加上代理
# proxy = {
#     "http":"http://127.0.0.1:8080",
#     "https":"https://127.0.0.1:8080"
# }
# resp = requests.get("https://httpbin.org/get",proxies=proxy)
# print(resp.text)

#5、发送get请求，加鉴权 --Basic Auth
# from requests.auth import HTTPBasicAuth
# respo = requests.get("https://api.github.com/user",auth=HTTPBasicAuth(username="Hunter",password="test123"))
# print(respo.text)


#post请求利用data传递请求参数
data = {"name":"Hunter","passwd":"test123"}
# res = requests.post("https://httpbin.org/post",data=data)
# print(res.text)

#post请求利用json传递请求参数,直接利用json关键字参数传参,它会自动编码
# res= requests.post("https://httpbin.org/post",json=data)
# print(res.text)

#将字典通过json.dumps函数转化为json字符串
re = requests.post("https://httpbin.org/post",data=json.dumps(data))
print(re.text)